<?php
/**
 * Created by PhpStorm.
 * User: jiaosuxin(Silas@sf-express.com)
 * Date: 2019-07-15
 * Time: 17:39
 */

class ExplainHelper
{
    private static $start = 0;

    public static function start() {
        self::$start = time();
        echo "初始: " . self::sizeFormat(memory_get_usage()) . "\n";
    }

    public static function end() {
        echo '耗时：'. (time() - self::$start) . '秒' . "\n";

        echo "使用: ". self::sizeFormat(memory_get_usage())."\n";
        echo "峰值: ". self::sizeFormat(memory_get_peak_usage())."\n";
    }

    public static function sizeFormat($size) {
        if ($size / (1024 * 1024 * 1024) > 1) {
            return floatval($size / (1024 * 1024 * 1024)) . 'G';
        }

        if ($size / (1024 * 1024) > 1) {
            return floatval($size / (1024 * 1024)) . 'M';
        }

        if ($size / (1024) > 1) {
            return floatval($size / (1024)) . 'K';
        }
    }
}